package afcp.alumni.dao;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import afcp.alumni.dao.base.DaoDatastore;
import afcp.alumni.model.base.DatastoreEntity;
import afcp.alumni.model.db.Role;
import afcp.alumni.model.db.RolesAncestor;

public class RoleDao extends DaoDatastore<Role> {

	private static final Log logger = LogFactory.getLog(RoleDao.class);

	@Override
	protected Class<Role> getClassObject() {
		return Role.class;
	}
	
	private DatastoreEntity rolesAncestor;
	public static final String ANCESTOR_ID = "ROLES";

	@Override
	protected DatastoreEntity getEntityAncestor() {
		logger.info("getting roles ancestor");
		if (rolesAncestor == null) {
			logger.info("roles ancestor is null, getting the roles ancestor from datastore.");
			rolesAncestor = rolesDao.find(ANCESTOR_ID);
			if (rolesAncestor == null) {
				logger.info("roles ancestor is not in datastore. Creating Roles entity.");
				RolesAncestor ancestor = new RolesAncestor();
				rolesAncestor = ancestor;
				rolesDao.addOrUpdate(ancestor);
			}
		}
		return rolesAncestor;
	}

	private RoleDao() {
	}

	private static RoleDao instance = null;

	public static RoleDao getDao() {
		if (instance == null) {
			instance = new RoleDao();
		}
		return instance;
	}
	
	private RolesDao rolesDao = RolesDao.getDao();
	
//	@Override
//	public boolean add(Role entity) {
//		entity.setKey(getEntityAncestor().getKey());
//		return super.add(entity);
//	}

}
